package com.bric.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 读取配置文件工具类
 * ClassName: MyProperties
 * @Description: TODO
 * @author dingpeng
 * @date 2016-4-20
 */
public class MyProperties {
	
	
	public static boolean writePropertiesFile(String pro_url,List<Map<String,String>> map){
		boolean flag=false;
		Properties por = new Properties();
		OutputStream fos = null;
		InputStream fis = null;
		// File file = new File("dll\\configinf.dll");
		try {
			fis = new FileInputStream(pro_url);
			por.load(fis);
			fis.close();
			for(Map<String,String> mm:map){
				for(String key:mm.keySet()){
					por.setProperty(key, mm.get(key));
					fos = new FileOutputStream(pro_url);
					por.setProperty(key, mm.get(key));
				}
				}
			por.store(fos, "Update  value");
			fos.flush();
			flag=true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
				fis.close();
			} catch (IOException e) {
			}

		}
		return flag;
	}
	/**
	 * 读取pro_url配置文件类
	 * @author dingpeng
	 * @date 2016-4-20
	 */
	public static Properties loadProperty(String pro_url) {
        Properties prop=new Properties();
        try {
        	InputStream is= MyProperties.class.getResourceAsStream(pro_url);
            prop.load(is);
            is.close();
        } catch (IOException e) {
        }
        return prop;
    }
	/**
	 * 读取pro_url配置文件类
	 * @author bric_yangyulin
	 * @date 2017-04-14
	 * @param pro_url 文件路径
	 * @param encoding 编码
	 */
	public static Properties loadProperty(String pro_url, String encoding) {
        Properties prop=new Properties();
        try {
        	InputStream is= MyProperties.class.getResourceAsStream(pro_url);
        	prop.load(new InputStreamReader(is, "utf-8"));
            is.close();
        } catch (IOException e) {
        }
        return prop;
    }
	public static void main(String[] args) {
		/*Map<String,String> map=new HashMap<String, String>();
		map.put("aaa", "111111");
		List<Map<String,String>> list=new ArrayList<Map<String,String>>();
		list.add(map);
		writePropertiesFile("/chinaBric.properties", list);*/
		System.out.println(MyProperties.loadProperty("/chinaBric.properties").getProperty("fgwSendMessageMethodName"));
	}
}
